/// @tags:
#include <cctype>
#include <cstdio>
#include <cstring>

int const N = 100;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

int n, m, T, tota, totb;
int row[N], col[N];
char a[N][N], b[N][N];

inline bool check() {
  for (int i = 1; i <= n; ++i) {
    if (row[i] > 1) {
      for (int j = 1; j <= m; ++j)
        if (a[i][j] != b[i][j]) return false;
    }
  }
  for (int j = 1; j <= m; ++j) {
    if (col[j] > 1) {
      for (int i = 1; i <= n; ++i)
        if (a[i][j] != b[i][j]) return false;
    }
  }
  if (tota == m * n && totb != m * n) return false;
  return true;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("matrix.in", "r", stdin);
  freopen("matrix.out", "w", stdout);
#endif
#endif

  read(T);
  while (T--) {
    read(n), read(m);
    memset(row, 0, sizeof(row));
    memset(col, 0, sizeof(col));
    tota = 0, totb = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%s", a[i] + 1);
      for (int j = 1; j <= m; ++j)
        if (a[i][j] == '1') ++tota;
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%s", b[i] + 1);
      for (int j = 1; j <= m; ++j) {
        if (b[i][j] == '1') row[i]++, col[j]++, ++totb;
      }
    }
    puts(check() ? "Yes" : "No");
  }
  return 0;
}